home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
clang
/
dtk_demo.zip
/
NWEEKDAY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-12
|
2KB
|
100 lines
/* NWEEKDMN.C
* calculates the date of the nth Tuesday,
* or whatever, in a given month
* last mod.: 12-AUG-91
*/
#include <STDIO.H>
#include <STDLIB.H>
#include <STRING.H>
#include <L_DATE.H>
#include <L_STR.H>
char *usage = "\nUse: NWEEKDAY n day_name month year"
"\nThis gives the date of the nth Friday or whatever"
"\nfor the specified month and year in the Gregorian calendar."
"\nThe day name can be abbreviated, e.g. NWEEKDAY 3 Fri 12 2012.";
Uchar weekday[20];
Uchar date_str[64];
/*----------------------------*/
void main(int argc, char **argv)
{
int n, dow, specified_dow;
Date date;
Date_format df; /* for Date and Date_format see L_AUXSTR.H */
if ( argc < 5 )
{
printf(usage);
exit(0);
}
n = atoi(argv[1]);
strcpy(weekday,argv[2]);
strlwr(weekday);
capitalize(weekday,0);
date.day = 1;
date.month = atoi(argv[3]);
date.year = atol(argv[4]);
if ( n < 1 || n > 5 )
{
printf("\nInvalid n parameter.\n");
exit(1);
}
if ( !date_valid(&date) )
{
printf("\nInvalid month.\n");
exit(2);
}
/* convert name of day of week to an integer
* 0 = Sunday, 6 = Saturday
*/
dow = 0;
while ( dow < 7 && !strstr((Str_ptr)day_name(dow),weekday) )
dow++;
if ( dow > 6 )
{
printf("\nName of weekday is invalid.\n");
exit(3);
}
else
specified_dow = dow;
/* calculate day of week of 1st day of month/year */
dow = day_of_week(&date);
while ( dow != specified_dow )
{
dow = (++dow)%7;
date.day++;
}
/* date now contains the date of
* the 1st such weekday in the month
*/
date.day += 7*(n-1);
if ( date_valid(&date) )
{
set_date_format_default(&df);
df.commas_in_year = TRUE;
df.short_year = TRUE;
date_to_str(&date,&df,date_str);
printf("\nThe %d%s %s of the %d%s month of the year %ld is %s.\n",
n,ordinal_suffix(n),day_name(dow),date.month,
ordinal_suffix(date.month),date.year,date_str);
}
else
printf("\nThere is no %d%s %s of the %d%s month of the year %ld.\n",
n,ordinal_suffix(n),day_name(dow),date.month,
ordinal_suffix(date.month),date.year);
}